package com.itheima.edu.info.manager.service;

import com.itheima.edu.info.manager.dao.StudentDao;
import com.itheima.edu.info.manager.domain.Student;

public class StudentService {
    private static StudentDao studentDao = new StudentDao();

    public static boolean isExists(String id) {
        //创建学生数组对象，用Dao对象调用find方法,返回学生数组
        Student[] stu = studentDao.findALLStudent();     //
        boolean exists = false;
        for (int i = 0; i < stu.length; i++) {
            Student stu2 = stu[i];
            if (stu2 != null && stu2.getId().equals(id)) {
                exists = true;
                break;
            }

        }
        return exists;
    }//判断学号是否存在

    public static Student[] findStu() {            //查看学生代码开始
        Student[] stuSer = studentDao.findALLStudent();
        //只要判断Dao里面传来的数组,不为NULL,则直接返回这个数组,为NULL返回NULL
        boolean flag = false;
        for (int i = 0; i < stuSer.length; i++) {
            Student stud = stuSer[i];
            if (stud != null) {
                flag = true;
                break;
            }
        }
        if(flag){
            return stuSer;
        }else{
            return null;
        }
        /*
        boolean flag = false;
        int count = 0;
        for (int i = 0; i < stuSer.length; i++) {
            if (stuSer[i] != null) {
                count++;
                flag = true;
            }
        }
        Student[] stuSre1 = new Student[count];
        for (int i = 0; i < stuSer.length; i++) {
            if (stuSer[i] != null) {
                stuSre1[i] = stuSer[i];
            }
        }
        if (flag) {
            return stuSre1;
        } else {
            return null;
        }
        //查看结束*/
    }   //查看学生代码

    public static void deleteId(String id) {
        studentDao.deleteId(id);
    }

    public boolean addStudent(Student stu) {
        //建立一个Dao仓库
        //  StudentDao studentDao = new StudentDao();
        //仓库调用ADD方法
        //
        //因为是controller调用业务员的add方法,直接return返回值
        return studentDao.addStudent(stu);
    } //添加学生代码

    public void updateStudent(String id,String name,String age,String birthday) {
        studentDao.updateStuent(id,name,age,birthday);

    }
}


